

package com.hazelcast.client.impl.protocol.task;

import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.instance.impl.Node;
import com.hazelcast.internal.namespace.NamespaceUtil;
import com.hazelcast.internal.nio.Connection;

import java.util.UUID;

/**
 * Base message task for listener registration tasks
 */
public abstract class AbstractAddListenerMessageTask<P> extends AbstractAsyncMessageTask<P, UUID> {

    protected AbstractAddListenerMessageTask(ClientMessage clientMessage, Node node, Connection connection) {
        super(clientMessage, node, connection);
    }

    @Override
    protected void processMessage() {
        // We always want to be Namespace-aware when adding listeners
        NamespaceUtil.runWithNamespace(nodeEngine, getUserCodeNamespace(), super::processMessage);
    }

    @Override
    protected Object processResponseBeforeSending(UUID response) {
        addDestroyAction(response);
        return response;
    }

    protected void addDestroyAction(UUID registrationId) {
        endpoint.addListenerDestroyAction(getServiceName(), getDistributedObjectName(), registrationId);
    }

    protected abstract String getUserCodeNamespace();
}
